package com.net.oil.user.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.user.bean.CheckPhoneBean;
import com.net.oil.user.contract.ResetLoginPasswordContract;
import com.net.oil.user.presenter.ResetLoginPasswordPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/4/2:14:08
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ResetLoginPasswordModel extends BaseModel implements ResetLoginPasswordContract.Model {

    private ResetLoginPasswordPresenter presenter;

    public ResetLoginPasswordModel(ResetLoginPasswordPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    /**
     * 验证手机号
     * @param phone 手机号
     */
    @Override
    public void checkPhone(final String phone) {
        AppHttpClient.ApiService().checkPhone(phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CheckPhoneBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<CheckPhoneBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.checkPhoneSuccess(tResultBean.getData());
                        }else {
                            presenter.checkPhoneFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.checkPhoneFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    /**
     * 获取验证码
     * @param phone 手机号
     * @param ptt   是否是语音
     */
    @Override
    public void getPhoneCode(String phone, boolean ptt) {
        AppHttpClient.ApiService().getPhoneCode(phone, ptt)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT(){
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.getPhoneCodeSuccess();
                        }else {
                            presenter.getPhoneCodeFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.getPhoneCodeFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    /**
     * 验证验证码
     * @param phone 手机号
     * @param code  验证码
     */
    @Override
    public void verifyCode(String phone, String code) {
        AppHttpClient.ApiService().verifyCode(phone, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.verifyCodeSuccess();
                        }else {
                            presenter.verifyCodeFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.verifyCodeFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    /**
     * 重置密码
     * @param phone 手机号
     * @param code  验证码
     * @param newPassword   新密码
     */
    @Override
    public void resetLoginPassword(final String phone, String code, String newPassword) {
        AppHttpClient.ApiService().resetPassword(phone, code, newPassword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.resetLoginPasswordSuccess();
                        }else {
                            presenter.resetLoginPasswordFailed();
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.resetLoginPasswordFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
